package org.lex.ftp.ui.actions;

import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JDialog;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.TableColumnModel;

import org.lex.ftp.FTP;
import org.lex.ftp.ui.SessionsTableModel;
import org.lex.swing.ActionBuilder;
import org.lex.swing.util.Buttons;
import org.lex.swing.util.CenterSouthPane;
import org.lex.swing.widgets.EnhancedDialog;


public class SessionsAction extends FTPAction {
	public SessionsAction(FTP app) {
		super(app);
		this.setName("Sessions");
	}

	public void actionPerformed(ActionEvent e) {
		JDialog dialog = this.createDialog(app.getRootWindow());
		dialog.setSize(300, 220);
		dialog.setLocationRelativeTo(app.getRootWindow());
		dialog.setVisible(true);
	}

	private JDialog createDialog(Window root) {
		// main dialog
		final EnhancedDialog dialog = new EnhancedDialog(root, "Sessions");
		// control pane
		Buttons buttons = Buttons.trailing(new ActionBuilder("OK").setActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dialog.dispose();
			}
		}));
		// content pane
		dialog.setContentPane(new CenterSouthPane(new JScrollPane(createTable()), buttons));
		dialog.setDefaultButton(buttons.getButton(0));
		return dialog;
	}

	private static final int[] widths = { 35, 300 };

	private JTable createTable() {
		JTable table = new JTable(SessionsTableModel.getInstance());
		table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		TableColumnModel model = table.getColumnModel();
		for (int i = 0; i < widths.length; i++)
			model.getColumn(i).setPreferredWidth(widths[i]);
		return table;
	}
}
